#!/usr/bin/env python
'''The pen.pyw example has two classes which allows this to be update do I need to do the same?
or maybe I can pass a dictionary to pass the values'''


import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from plotDataClass import LinePlotClass as LPC

import ui_Plot_Options

#markers = {0: '_draw_tickleft', 1: '_draw_tickright', 2: '_draw_tickup', 
#3: '_draw_tickdown', 4: '_draw_caretleft', 5: '_draw_caretright', 
#filled_markers = ('o', '^', 'v', '<', '>', 's', 'd', 'D', 'h', 'H', 'p')

line_styles = dict(Solid = '-',\
                    Dashed = '--', \
                    DashDot = '-.',\
                    Dotted = ':',\
                    Steps = 'steps')

marker_styles = dict(none = '',\
                    circles = 'o',\
                    triangle_up = '^',\
                    triangle_down  = 'v',\
                    triangle_left  = '<',\
                    triangle_right  = '>',\
                    square  = 's',\
                    plus  = '+',\
                    cross  = 'x',\
                    diamond  = 'D',\
                    thin_diamond  = 'd',\
                    tripod_down  = '1',\
                    tripod_up  = '2',\
                    tripod_left  = '3',\
                    tripod_right  = '4',\
                    hexagon  = 'h',\
                    rotated_hexagon  = 'H',\
                    pentagon  = 'p',\
                    vertical_line  = '|',\
                    horizontal_line  = '_',\
                    dots = '.')


class Plot_Options_Dialog(QDialog, ui_Plot_Options.Ui_Plot_Options_Dialog):
    def __init__(self, activeTraces = None, parent = None):
        super(Plot_Options_Dialog, self).__init__(parent)
        self.setupUi(self)
        
        self.plotTitle = "Plot Title"
        self.xmin = 0.0
        self.xmax = 10.0
        self.xtitle = "X-Axis"
        
        self.ymin = 0.0
        self.ymax = 20.0
        self.ytitle = "Y-Axis"
        self.activeplots = activeTraces#self.getPlotDict(activeTraces)
        #print len(activeTraces)
                
        #self.connect(self.btn_SelectColor, SIGNAL("clicked()"), self.getColor)
        self.connect(self.activePlotListWidget, SIGNAL("itemClicked(QListWidgetItem *)"), self.getPlotItem)
        self.connect(self.mstyle_comboBox, SIGNAL("currentIndexChanged(QString)"), self.setMStyle)
        self.connect(self.ms_spinBox, SIGNAL("valueChanged(double)"), self.setMSize)
        self.connect(self.lstyle_comboBox, SIGNAL("currentIndexChanged(QString)"), self.setLStyle)
        self.connect(self.lw_spinBox, SIGNAL("valueChanged(double)"), self.setLWidth)
        self.connect(self.tractColorButton, SIGNAL("colorChanged(QColor)"), self.setLColor) 
        
        self.populate_dialog()
        
    def setMSize(self, value):
        #print "Set Marker Size"
        if self.activePlotListWidget.currentItem():
            activeLinePlot = self.getPlotItem(self.activePlotListWidget.currentItem())
            activeLinePlot.set_markerSize(value)
            self.setupLineOptions(activeLinePlot)
        
    def setMStyle(self, value):
        #print "Set Marker Style"
        if self.activePlotListWidget.currentItem():
            activeLinePlot = self.getPlotItem(self.activePlotListWidget.currentItem())
            activeLinePlot.set_markerStyle(marker_styles.get(str(value)))#need to set value to str as it is QString
            self.setupLineOptions(activeLinePlot)
            
    def setLWidth(self, value):
        #print "Set L Width"
        if self.activePlotListWidget.currentItem():
            activeLinePlot = self.getPlotItem(self.activePlotListWidget.currentItem())
            activeLinePlot.set_lineWidth(value)
            self.setupLineOptions(activeLinePlot)
    
    def setLStyle(self, value):
        #print "Set Line Style"
        if self.activePlotListWidget.currentItem():
            activeLinePlot = self.getPlotItem(self.activePlotListWidget.currentItem())
            activeLinePlot.set_lineStyle(line_styles.get(str(value)))#need to set value to str as it is QString
            self.setupLineOptions(activeLinePlot)
    
    def setLColor(self, QColor):
        if self.activePlotListWidget.currentItem():
            activeLinePlot = self.getPlotItem(self.activePlotListWidget.currentItem())
            activeLinePlot.set_lineColor(str(QColor.name()))#need to set value to str as it is QString
            #self.setupLineOptions(activeLinePlot)
        
    def getPlotItem(self, item):
        if type(self.activeplots.get(str(item.text()))[1]) is LPC:
            linePlotItem = self.activeplots.get(str(item.text()))[1]
            self.setupLineOptions(linePlotItem)
            return linePlotItem
            
    def getKey(self, dict, value):
        for item in dict.iteritems():
            if value is item[1]:
                return item[0]
    
    def setupLineOptions(self, lineInstance):
        match_index = self.lstyle_comboBox.findText(self.getKey(line_styles, lineInstance.lineStyle))
        self.lstyle_comboBox.setCurrentIndex(match_index)
        
        match_index = self.mstyle_comboBox.findText(self.getKey(marker_styles, lineInstance.markerStyle))
        self.mstyle_comboBox.setCurrentIndex(match_index)
        
        self.lw_spinBox.setValue(lineInstance.lineWidth)
        self.ms_spinBox.setValue(lineInstance.markerSize)
        
        if lineInstance.lineColor is None:
            pass
            #print "Color set to default"
##            lcolor = QColor('#FFFFFF')
##            self.tractColorButton.setColor(lcolor)
        else:
            lcolor = QColor(lineInstance.lineColor)
            self.tractColorButton.setColor(lcolor)
            
    def getPlotDict(self, activeTraces):#takes a list of LinePlotClasses
        tuple_list = []
        if activeTraces:
            for item in activeTraces.__iter__():
                tuple_list.append((item.label, item))
        
        return dict(tuple_list)
            
            
    def getColor(self):    
        color = QColorDialog.getColor(Qt.blue, self)
        if color.isValid(): 
            print color.name()
            #self.colorLabel.setText(color.name())
            #self.colorLabel.setPalette(QtGui.QPalette(color))
    
            
    def populate_dialog(self):
        print "Go"
        #populate first tab
        self.plottitle_lineEdit.setText(self.plotTitle)
        
        self.xmin_lineEdit.setText(str(self.xmin))
        self.xmax_lineEdit.setText(str(self.xmax))
        self.xlabel_lineEdit.setText(self.xtitle)
        
        self.ymin_lineEdit.setText(str(self.ymin))
        self.ymax_lineEdit.setText(str(self.ymax))
        self.ylabel_lineEdit.setText(self.ytitle)
        
        #populate second tab
        self.lstyle_comboBox.addItems(line_styles.keys())
        self.lstyle_comboBox.setCurrentIndex(2)        
        self.mstyle_comboBox.addItems(marker_styles.keys())
        
        if self.activeplots is not None and type(self.activeplots) is dict:
            activeList = self.activeplots.keys()
            activeList.sort()
            print activeList
            self.activePlotListWidget.addItems(activeList)
        self.activePlotListWidget.setCurrentRow(0)
        self.getPlotItem(self.activePlotListWidget.currentItem())#gets current item in list
    
    
    
if __name__ == "__main__":
    dialog = QApplication(sys.argv)
    mydialog = Plot_Options_Dialog()
    mydialog.show()
    sys.exit(dialog.exec_())